package com.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Comment;
import com.entity.Contact;
import com.service.ContactService;

public class XmlServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		response.setCharacterEncoding("UTF-8");
		
		response.setContentType("text/xml;charset=UTF-8");
		String id=request.getParameter("contactid");
		
		ContactService cs=new ContactService();
		List<Comment> list=cs.findComment(id);
		
		Contact c=cs.findContactById(id);
		request.setAttribute("c", c);
		
		PrintWriter out = response.getWriter();
		
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.print("<contacts>");
		out.print("<contact>");
		out.print("<id>");
		out.print(c.getId());
		out.print("</id>");
		out.print("<name>");
		out.print(c.getName());
		out.print("</name>");
		out.print("<company>");
		out.print(c.getCompany());
		out.print("</company>");
		out.print("<tel>");
		out.print(c.getTel());
		out.print("</tel>");
		out.print("<phone>");
		out.print(c.getPhone());
		out.print("</phone>");
		out.print("<email>");
		out.print(c.getEmail());
		out.print("</email>");
		out.print("<address>");
		out.print(c.getAddress());
		out.print("</address>");
		out.print("<home>");
		out.print(c.getHome());
		out.print("</home>");
		out.print("<weibo>");
		out.print(c.getWeibo());
		out.print("</weibo>");
		out.print("<adstruct>");
		out.print(c.getAdstruct());
		out.print("</adstruct>");
		out.print("<createtime>");
		out.print(c.getCreatetime());
		out.print("</createtime>");
		out.print("<comments>");
		for(Comment ct:list){
			out.print("<comment>");
			out.print("<message>");
			out.print(ct.getMessage());
			out.print("</message>");
			out.print("<createTime>");
			out.print(ct.getCreateTime());
			out.print("</createTime>");
			out.print("<username>");
			out.print(ct.getUser().getUserName());
			out.print("</username>");
			out.print("</comment>");
		}
		out.print("</comments>");
		out.print("</contact>");
		out.print("</contacts>");
	}
}
